//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

import SmithyEventStreamsAPI
import ClientRuntime
import Foundation

enum TestEvent {
    case allHeaders
    case emptyPayload
    case noHeaders

    static var unmarshal: UnmarshalClosure<TestEvent> {
        { message in
            if message == EventStreamTestData.validMessageWithAllHeaders() {
                return .allHeaders
            } else if message == EventStreamTestData.validMessageEmptyPayload() {
                return .emptyPayload
            } else if message == EventStreamTestData.validMessageNoHeaders() {
                return .noHeaders
            } else {
                fatalError("Unknown message")
            }
        }
    }

    static var marshal: MarshalClosure<TestEvent> {
        { (self) in
            switch self {
            case .allHeaders:
                return EventStreamTestData.validMessageWithAllHeaders()
            case .emptyPayload:
                return EventStreamTestData.validMessageEmptyPayload()
            case .noHeaders:
                return EventStreamTestData.validMessageNoHeaders()
            }
        }
    }
}

struct EventStreamTestData {
    static func validMessageWithAllHeaders() -> Message {
        return Message(
            headers: [
                .init(name: "true", value: .bool(true)),
                .init(name: "false", value: .bool(false)),
                .init(name: "byte", value: .byte(50)),
                .init(name: "short", value: .int16(20_000)),
                .init(name: "int", value: .int32(500_000)),
                .init(name: "long", value: .int64(50_000_000_000)),
                .init(name: "bytes", value: .byteArray("some bytes".data(using: .utf8)!)),
                .init(name: "str", value: .string("some str")),
                .init(name: "time", value: .timestamp(Date(timeIntervalSince1970: 5_000_000))),
                .init(name: "uuid", value: .uuid(UUID(uuidString: "b79bc914-de21-4e13-b8b2-bc47e85b7f0b")!))
            ],
            payload: "some payload".data(using: .utf8)!
        )
    }

    static func validMessageDataWithAllHeaders() -> Data {
        /// This is the encoded version of the above message validMessageWithAllHeaders
        /// https://github.com/awslabs/aws-sdk-kotlin/blob/2e22b7bb25c3ec7b192f305e999d1e6f9c40368b/aws-runtime/protocols/aws-event-stream/common/test/aws/sdk/kotlin/runtime/protocol/eventstream/MessageTest.kt#L29
        return Data([
            0x00, 0x00, 0x00, 0x96, 0x00, 0x00, 0x00, 0x7a, 0x8b, 0xb4, 0x95, 0xfb, 0x04, 0x74, 0x72, 0x75,
            0x65, 0x00, 0x05, 0x66, 0x61, 0x6c, 0x73, 0x65, 0x01, 0x04, 0x62, 0x79, 0x74, 0x65, 0x02, 0x32,
            0x05, 0x73, 0x68, 0x6f, 0x72, 0x74, 0x03, 0x4e, 0x20, 0x03, 0x69, 0x6e, 0x74, 0x04, 0x00, 0x07,
            0xa1, 0x20, 0x04, 0x6c, 0x6f, 0x6e, 0x67, 0x05, 0x00, 0x00, 0x00, 0x0b, 0xa4, 0x3b, 0x74, 0x00,
            0x05, 0x62, 0x79, 0x74, 0x65, 0x73, 0x06, 0x00, 0x0a, 0x73, 0x6f, 0x6d, 0x65, 0x20, 0x62, 0x79,
            0x74, 0x65, 0x73, 0x03, 0x73, 0x74, 0x72, 0x07, 0x00, 0x08, 0x73, 0x6f, 0x6d, 0x65, 0x20, 0x73,
            0x74, 0x72, 0x04, 0x74, 0x69, 0x6d, 0x65, 0x08, 0x00, 0x00, 0x00, 0x01, 0x2a, 0x05, 0xf2, 0x00,
            0x04, 0x75, 0x75, 0x69, 0x64, 0x09, 0xb7, 0x9b, 0xc9, 0x14, 0xde, 0x21, 0x4e, 0x13, 0xb8, 0xb2,
            0xbc, 0x47, 0xe8, 0x5b, 0x7f, 0x0b, 0x73, 0x6f, 0x6d, 0x65, 0x20, 0x70, 0x61, 0x79, 0x6c, 0x6f,
            0x61, 0x64, 0x01, 0xa0, 0x58, 0x60,
        ])
    }

    static func validMessageEmptyPayload() -> Message {
        return Message(
            headers: [
                .init(name: "some-header", value: .int16(500)),
            ]
        )
    }

    static func validMessageDataEmptyPayload() -> Data {
        /// This is the encoded version of the above message validMessageEmptyPayload
        /// https://github.com/awslabs/aws-sdk-kotlin/blob/2e22b7bb25c3ec7b192f305e999d1e6f9c40368b/aws-runtime/protocols/aws-event-stream/common/test/aws/sdk/kotlin/runtime/protocol/eventstream/MessageTest.kt#L42
        return Data([
            0x00, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x00, 0x0f, 0x17, 0x2d, 0xc2, 0xab, 0x0b, 0x73, 0x6f, 0x6d,
            0x65, 0x2d, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x03, 0x01, 0xf4, 0x93, 0x3c, 0x5c, 0xad,
        ])
    }

    static func validMessageNoHeaders() -> Message {
        return Message(
            payload: "another test payload".data(using: .utf8)!
        )
    }

    static func validMessageDataNoHeaders() -> Data {
        /// This is the encoded version of the above message validMessageNoHeaders
        /// https://github.com/awslabs/aws-sdk-kotlin/blob/2e22b7bb25c3ec7b192f305e999d1e6f9c40368b/aws-runtime/protocols/aws-event-stream/common/test/aws/sdk/kotlin/runtime/protocol/eventstream/MessageTest.kt#L47
        return Data([
            0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00, 0x51, 0x63, 0x56, 0xad, 0x61, 0x6e, 0x6f, 0x74,
            0x68, 0x65, 0x72, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64,
            0x8d, 0xf8, 0x0e, 0x65,
        ])
    }

    static func validInitialResponseMessage() -> Message {
        return Message(
            headers: [
                .init(name: ":event-type", value: .string("initial-response")),
                .init(name: ":message-type", value: .string("event")),
                .init(name: ":content-type", value: .string("application/x-amz-json-1.1"))
            ],
            payload: "{\"someMetadata\": \"test\"}".data(using: .utf8)!
        )
    }

    static func validInitialResponseMessageData() -> Data {
        /// This is the encoded version of the above message validInitialResponseMessage
        return Data([
            0x00, 0x00, 0x00, 0x88, 0x00, 0x00, 0x00, 0x60, 0xa9, 0x06, 0x45, 0x62, 0x0b, 0x3a, 0x65, 0x76,
            0x65, 0x6e, 0x74, 0x2d, 0x74, 0x79, 0x70, 0x65, 0x07, 0x00, 0x10, 0x69, 0x6e, 0x69, 0x74, 0x69,
            0x61, 0x6c, 0x2d, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x0d, 0x3a, 0x6d, 0x65, 0x73,
            0x73, 0x61, 0x67, 0x65, 0x2d, 0x74, 0x79, 0x70, 0x65, 0x07, 0x00, 0x05, 0x65, 0x76, 0x65, 0x6e,
            0x74, 0x0d, 0x3a, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x74, 0x79, 0x70, 0x65, 0x07,
            0x00, 0x1a, 0x61, 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x78, 0x2d,
            0x61, 0x6d, 0x7a, 0x2d, 0x6a, 0x73, 0x6f, 0x6e, 0x2d, 0x31, 0x2e, 0x31, 0x7b, 0x22, 0x73, 0x6f,
            0x6d, 0x65, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0x22, 0x3a, 0x20, 0x22, 0x74, 0x65,
            0x73, 0x74, 0x22, 0x7d, 0xe6, 0x23, 0x00, 0xf1,
        ])
    }
}
